package com.yanqu.road.web.controller.usercenter.newadmin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.admin.AdminResult;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 50用户订单查询
 */
@RestController
@RequestMapping("/usercenter/admin")
public class CenterAdminOrderController {

    /**
     * 50.1 订单查询
     */
    @ResponseBody
    @RequestMapping(value = "allOrder/search", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String allOrderSearch(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        long serverId = 0;
        JSONArray serverIds = jsonObject.getJSONArray("serverIds");
        for (int i = 0; i < serverIds.size(); i++) {
            serverId = serverIds.getLongValue(i);
            break;
        }
        if(serverId <= 0){
            return AdminResult.failMsg("区服参数缺失").toJsonString();
        }

        ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);

        String response = HttpHelper.doPostJson(serverInfo.getWebServerUrl() + "/game/admin/allOrder/search", jsonObject.toJSONString(), "UTF-8");
        if (StringUtils.isNullOrEmpty(response)) {
            return AdminResult.failMsg("网络失败").toJsonString();
        }
        if (HttpHelper.ERROR.equals(response)) {
            return AdminResult.failMsg("网络失败").toJsonString();
        }

        return response;
    }

    /**
     * 50.2订单导出
     */
    @ResponseBody
    @RequestMapping(value = "allOrder/export", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String allOrderExport(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        long serverId = 0;
        JSONArray serverIds = jsonObject.getJSONArray("serverIds");
        for (int i = 0; i < serverIds.size(); i++) {
            serverId = serverIds.getLongValue(i);
            break;
        }
        if(serverId <= 0){
            return AdminResult.failMsg("区服参数缺失").toJsonString();
        }

        ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);

        String response = HttpHelper.doPostJson(serverInfo.getWebServerUrl() + "/game/admin/allOrder/export", jsonObject.toJSONString(), "UTF-8");
        if (StringUtils.isNullOrEmpty(response)) {
            return AdminResult.failMsg("网络失败").toJsonString();
        }
        if (HttpHelper.ERROR.equals(response)) {
            return AdminResult.failMsg("网络失败").toJsonString();
        }

        return response;
    }
}
